package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GirlsController {
    @Autowired
    private   GirlRepository girlRepository;
    @Autowired
    private  GirlServers girlServers;
    /**
     * 查询所有女生列表
     * @return
     */
    @GetMapping(value = "/girls")
    public List<Girl> girlList(){
          return girlRepository.findAll();
    }

    /**
     * 添加一个女生
     * @param cupSize
     * @param age
     * @return
     */
    @PostMapping(value = "/girls")

    public Girl addGirl(@RequestParam("cupSize") String cupSize,
                           @RequestParam("age") Integer age){
        Girl  girl=new Girl();
        girl.setCupSize(cupSize);
        girl.setAge(age);

        return  girlRepository.save(girl);

    }
    /**
     * 查询一个女生
     */
    @GetMapping(value = "/girls/{id}")
     public  Girl FindoneGirl(@PathVariable("id") Integer id){

               return girlRepository.findOne(id);

    }

    /**
     * 更新一个女生信息
     */
    public Girl updateGirl(@PathVariable("id") Integer id,
                           @RequestParam("cupSize") String cupSize,
                           @RequestParam("age") Integer age){

        Girl girl =new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);

        return    girlRepository.save(girl);


    }
    @PutMapping(value="/girls/{id}")
    /**
     * 删除一个女生信息
     */

   @DeleteMapping(value = "/girls/{id}")
    public void DeleteGirl(@PathVariable("id") Integer id){
        girlRepository.delete(id);
   }

   //通过年龄来查询女生列表
   @GetMapping(value = "/girls/age/{age}")
public  List<Girl> girlListByAge(@PathVariable("age") Integer age){
        return girlRepository.findByAge(age);
    }
    @PostMapping(value = "/girls/two")
    public void  addTwoGirls(){
        girlServers.insertTwo();
    }
}
